﻿Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Security
    Public Shared Function GenerateFileMD5(filepath As String) As String
        Try
            Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
            Dim f As FileStream = New FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

            f = New FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            md5.ComputeHash(f)
            f.Close()

            Dim hash As Byte() = md5.Hash
            Dim buff As StringBuilder = New StringBuilder
            Dim hashByte As Byte

            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next

            Dim md5string As String = buff.ToString()

            Return md5string
        Catch
            Return ""
        End Try
    End Function
End Class
